package controllers;

import utils.DAOUser;

import java.io.IOException;
import models.BeanUser;
import utils.BeanUtilities;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class RegistrationController
 */
public class RegistrationController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RegistrationController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		try {
			if (request.getParameter("activationCode") == null) {
				// Usuari nou.
				DAOUser daoUser = new DAOUser();
				BeanUser user = new BeanUser();
				BeanUtilities.populateBean(user, request);
				if (user.isComplete()) {
					daoUser.insertUserSQL(user);

					// Send "e-mail".
					// request.setAttribute("activationURL",
					// "/Practica2/RegistrationController?user=" +
					// user.getUser()+"&activationCode=" + activationCode);

					String username = user.getUser();

					String pass = user.getPassword();

					request.setAttribute("user", username);
					request.setAttribute("password", pass);
					request.setAttribute("message", "Thanks for registering!");
					RequestDispatcher dispatcher = request
							.getRequestDispatcher("LoginController");
					if (dispatcher != null)
						dispatcher.forward(request, response);
				} else {
					// Form not submitted correctly.
					request.setAttribute("user", user);
					request.setAttribute("registre", true);
					RequestDispatcher dispatcher = request
							.getRequestDispatcher("/index.jsp");
					if (dispatcher != null)
						dispatcher.forward(request, response);
				}
			} else {
				// Check activation code.
				String activationCode = request.getParameter("activationCode");
				activationCode = activationCode.replace(' ', '+');
				String user = request.getParameter("user");
				DAOUser daoUser = new DAOUser();
				if (!daoUser.userExists(user)) {
					// L'usuari no existeix..
					request.setAttribute("message", "User does not exist");
					RequestDispatcher dispatcher = request
							.getRequestDispatcher("home.jsp");
					if (dispatcher != null)
						dispatcher.forward(request, response);
				} else if (daoUser.isUserActivated(activationCode)) {
					// L'usuari ja s'havia activat previament.
					request.setAttribute("message", "User is already activated");
					RequestDispatcher dispatcher = request
							.getRequestDispatcher("home.jsp");
					if (dispatcher != null)
						dispatcher.forward(request, response);
				} else {
					// L'usuari s'ha activat correctament.
					daoUser.setUserActivated(activationCode);
					HttpSession session = request.getSession();
					synchronized (session) {
						session.setAttribute("user", user);
					}
					request.setAttribute("message", "Thank you for activating");

					RequestDispatcher dispatcher = request
							.getRequestDispatcher("/home.jsp");
					if (dispatcher != null)
						dispatcher.forward(request, response);
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			response.sendRedirect("blank.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
